// @algorithm @lc id=36 lang=cpp 
// @title valid-sudoku


#define print(...)
// @test([["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]])=true
// @test([["8","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]])=false
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        auto n = board.size();
        vector<vector<map<char,int>>> ss(3, vector<map<char,int>>(n, map<char,int>()));
        for(int i=0; i<n ;i++){
            for(int j=0; j<n; j++){
                char c = board[i][j];
                if(c == '.') continue;
                if(++ss[0][i][c] > 1){
                    print("[", i, "][", j, "]:row=", c, "\n")
                    return false;
                }
                if(++ss[1][j][c] > 1){
                    print("[", i, "][", j, "]:col=", c, "\n")
                    return false;
                }
                if(++ss[2][i/3*3+j/3][c] > 1){
                    print("[", i, "][", j, "]:box[", i/3*3+j/3, "]=", c, "\n")
                    return false;
                }
            }
        }
        return true;
    }
};